<div class="container-fluid">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">SKU质检标准确认</h3></div>
            <div class="panel-body">
                <div class="container-fluid">
                    <div class="col-sm-12 col-md-3  add-space">
                        <div class="input-group input-group-md">
                            <span class="input-group-addon">批次号</span>
                            <input class="form-control" type="text" name="batchCode" id="batchCode"
                                   onkeyup="this.value=this.value.replace(/\s/g,'')">
                        </div>
                    </div>
                    <div class="col-sm-12 col-md-3  add-space">
                        <div class="input-group input-group-md">
                            <span class="input-group-addon">确认状态</span>
                            <select type="text" name="skuCheckSign" class="form-control" id="skuCheckSign">
                                <option value="">请选择</option>
                                <option value="10">待确认</option>
                                <option value="-1">无需确认</option>
                                <option value="20">已确认</option>
                            </select>
                        </div>
                    </div>

                    <div class="col-sm-12 col-md-3 add-space">
                        <div class="input-group input-group-md">
                            <span class="input-group-addon">收货明细号</span>
                            <input type="text" class="form-control" name="recieveDetailsId" id="recieveDetailsId"
                                   onkeyup="this.value=this.value.replace(/\D/g,'')"/>
                        </div>
                    </div>
                    <div class="col-md-2 add-space">
                        <input class="btn btn-primary" type="button" value="查询" onclick="getInfo()">
                    </div>


                </div>
                <div class="clearfix"></div>
                <hr>
                <table class="table table-striped  table-hover" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th width="20"><input id="checkAll" type="checkbox"></th>
                        <th>站点</th>
                        <th>批次号</th>
                        <th>收货明细单号</th>
                        <th>收货人</th>
                        <th>采购单号</th>
                        <th>退税类型</th>
                        <th>采购员</th>
                        <th>SKU</th>
                        <th>采购名称</th>
                        <th>质检标准</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="txtHint">
                    </tbody>
                </table>
                <div class="clearfix clear"></div>
                <div class="col-md-6 pull-left">
                    <button class="btn btn-info btn-sm" id="patchcheck" onclick="batchProcessing()">批量确认</button>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    /**
     * 根据收货明细单号获取详情
     */
    function getInfo() {
        var recieveDetailsId = $("#recieveDetailsId").val();
        var batchCode = $("#batchCode").val();
        var skuCheckSign = $("#skuCheckSign option:selected").val();

        if ('' == recieveDetailsId && '' == batchCode) {
            layer.msg('请填入收货明细单号或者批次号',{'icon':'5'});
            return;
        }

        $.ajax({
            url: "__URL__/check_standard_index",
            data: {'recieveDetailsId': recieveDetailsId, 'skuCheckSign': skuCheckSign, 'batchCode': batchCode},
            type: 'get',
            dataType: 'json',
            async: true,

            success: function (data) {
                if (false == data) {
                    layer.msg('该收货明细单不存在!',{'icon':'5'});
                    $("#txtHint").html('<tr></tr>');
                    return;
                }

                var obj = (new Function('return ' + data + ';'))();
                var strTem = '';
                var button = '';

                for (key in obj['result']) {
                    if ('0' == obj['result'][key]['skuchecksign']) {
                        button = '<span>无需确认</span>';
                    } else if (10 == obj['result'][key]['skuchecksign']) {
                        button = '<button type="button" class="btn btn-info btn-sm" id="check' + obj['result'][key]['id'] + '" ' +
                                'onclick="checkSingleSkuStandard(' + obj['result'][key]['id'] + ')">质标确认</button>';
                    } else if (20 == obj['result'][key]['skuchecksign']) {
                        button = '<button type="button" class="btn btn-access btn-sm">已确认</button>';
                    }
                    if (null == obj['result'][key]['standard']) obj['result'][key]['standard'] = '';
                    strTem += '<tr>' +
                            '<td class="text-center"><input name="subBox" type="checkbox" value="' + obj['result'][key]['id'] + '"></td>' +
                            '<td>' + obj['result'][key]['site_id'] + '</td>' +
                            '<td>' + obj['result'][key]['batch_code'] + '</td>' +
                            '<td>' + obj['result'][key]['id'] + '</td>' +
                            '<td>' + obj['users'][obj['result'][key]['recieve_man']] + '</td>' +
                            '<td>' + obj['result'][key]['purchaseorder_id'] + '</td>' +
                            '<td>' + obj['rebate'][obj['result'][key]['export_tax_rebate']] + '</td>' +
                            '<td>' + obj['users'][obj['result'][key]['purchase_id']] + '</td>' +
                            '<td>' + obj['result'][key]['sku'] + '</td>' +
                            '<td>' + obj['result'][key]['sku_name'] + '</td>' +
                            '<td>' + obj['result'][key]['standard'] + '</td>' +
                            '<td>' + button + '</td>' +
                            '</tr>';
                }
                $("#txtHint").html(strTem); //结果展示到id为txtHint的标签中

            },

            error: function () {
                layer.msg('查询失败,请检查输入,无法解决请找IT',{'icon':'5'});
            }
        });

    }

    /**
     * sku质量标准确认
     */
    function checkSingleSkuStandard(recieveDetailId) {
        $.ajax({
            url: '__URL__/checkSkuStandard',
            data: {'recieveDetailId': recieveDetailId},
            type: 'post',
            dataType: 'json',
            async: true,

            beforeSend:function(){
                //请求前的处理
                $('#check'+recieveDetailId).text('正在确认...');
                $('#check'+recieveDetailId).attr('disabled','true');
            },

            success: function (data) {
                layer.msg(data,{'icon':'6'});
                getInfo();
            },

            error: function () {
                layer.msg('审核失败,请重试,无法解决请找IT',{'icon':'5'});
                getInfo();
            }
        });
    }

    /**
     * 批量确认质检标准
     */
    function batchProcessing() {
        var allChecked = new Array;
        $('input[name="subBox"]:checked').each(function (i) {
            allChecked[i] = $(this).val();
        });

        var ids = allChecked.join('-');
        if ('' == ids) {
            layer.msg('请先选择!',{'icon':'5'});
            return;
        }

        $.ajax({
            url: '__URL__/checkSkuStandard',
            data: {'recieveDetailId': ids},
            type: 'post',
            dataType: 'json',
            async: true,

            beforeSend:function(){
                //请求前的处理
                $('#patchcheck').text('正在确认...');
                $('#patchcheck').attr('disabled','true');
            },

            success: function (data) {
                layer.msg(data,{'icon':'6'});
                getInfo();
                $('#patchcheck').text('批量确认');
                $('#patchcheck').removeAttr('disabled');
            },

            error: function () {
                layer.msg('审核失败,请重试,无法解决请找IT',{'icon':'5'});
                getInfo();
                $('#patchcheck').text('批量确认');
                $('#patchcheck').removeAttr('disabled');
            }
        })
    }

    /**
     * 去掉字符串左右两侧的空格
     * @param str
     * @returns {void|string|XML}
     */
    function trim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
</script>